package com.infinit.easyreader.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.infinit.easyreader.R;
import com.infinit.easyreader.adpter.FontAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FontActivity extends AppCompatActivity {


    public static final String FONT_SYS = "系统字体";
    public static final String FONT_HY = "汉仪旗黑";
    public static final String FONT_FZ = "方正兰亭";
    public static final String FONT_RESULT = "fontResult";
    @Bind(R.id.fontList)
    ListView fontList;
    @Bind(R.id.ibFontBack)
    ImageButton ibFontBack;
    private List<String> listFont;
    private FontAdapter fontAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_font);
        ButterKnife.bind(this);
        setData();
        setAdapter();
        //获取初始信息
        String chosedFont = getIntent().getStringExtra("fontFamily");
        if (chosedFont.equals(FONT_SYS)) {
            fontAdapter.setCheckedPosition(0);
        } else if (chosedFont.equals(FONT_FZ)) {
            fontAdapter.setCheckedPosition(2);
        } else if (chosedFont.equals(FONT_HY)) {
            fontAdapter.setCheckedPosition(1);
        }
        setListener();
    }

    @OnClick(R.id.ibFontBack)
    public void goBack() {
        this.finish();
    }

    /**
     * 设置数据源
     */

    private void setData() {

        listFont = new ArrayList<>();
        listFont.add("系统字体");
        listFont.add("汉仪旗黑");
        listFont.add("方正兰亭");
    }


    /**
     * 设置适配器
     */

    private void setAdapter() {
        fontAdapter = new FontAdapter(listFont, this);
        fontList.setAdapter(fontAdapter);
    }


    /**
     * 设置监听器
     */
    private void setListener() {
        fontList.setOnItemClickListener(new fontListListener());
    }


    private class fontListListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //首先要确认其他的选项有没有被选中
            fontAdapter.setCheckedPosition(position);
            fontAdapter.notifyDataSetChanged();
            //将字体选择信息返回
            Intent intent = new Intent();
            String chosedFont = FONT_SYS;
            switch (position) {
                case 0:
                    chosedFont = FONT_SYS;
                    break;
                case 1:
                    chosedFont = FONT_HY;
                    break;
                case 2:
                    chosedFont = FONT_FZ;
                    break;
            }
            intent.putExtra(FONT_RESULT, chosedFont);
            setResult(RESULT_OK, intent);
            finish();
        }
    }


}
